﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

using ns_User;
using ns_SecurityManage;
using ns_DatabaseAccess;
using ns_FilesManage;
using ns_SessionManage;
using ns_FriendsList;

namespace ns_ServerOperation
{
    public interface i_ServerOperation 
    {
        //用户注册
       // Boolean Register(String name, String password, String question, String answer, String sex, String address, String email, String phone, String signWords, String imgHeadPath);
        //Boolean Register(String name, String password, String question, String answer, String sex, String address, String email, String phone);
        //Boolean Register(String name, String password, String question, String answer);
        Boolean Register(String name, String password);

        Boolean Login(String name, String password); //登录
        Boolean IsUserLogin(); //检查是否登录
        //Boolean Exit(String name); //安全退出

        //Boolean ChangePassword(String name, String password, String newPassword); //修改密码
       // Boolean ChangePasswordByAnswer(String name, String answer, String newPassword); //通过回答问题修改密码（忘记密码）
        //Boolean ChangeSex(String name,String sex);
        //Boolean ChangeSignWords(String name, String signWords);
        //Boolean ChangeEmail(String name, String email);

        //Boolean SendMSG(String contentXml);//发送聊天信息，content是xml格式，里面有详细信息
        //Boolean AddFriends(String name, String friendName, String friendType); //将friendName加到好友列表中,friendType为用户自定义的好友类型，默认为“我的好友”
        //String GetMSG(String name); //获取name的及时聊天信息，返回的是xml格式的字符串
        String GetFriendsList(); //获取好友列表，返回的是xml格式的字符串
        //String GetFriendsStates(String name); //获取好友状态，返回的是xml格式的字符串
    }
    public class c_ServerOperation : i_ServerOperation
    {
        
        public Boolean Register(String name, String password)
        {
            c_User user = new c_User();
            if (ns_DatabaseAccess.c_DBA.SqlDataAccess.IsUserExist(name)) return false;
            user.Name = name;
            user.Address = "unknow";
            user.Email = "unknow";
            user.HashedAnswer = ns_SecurityManage.c_Hash.Md5("unknow");
            user.HashedPassword = ns_SecurityManage.c_Hash.Md5(password);
            user.ID = ns_DatabaseAccess.c_DBA.SqlDataAccess.NewID();
            user.LastLogTime = DateTime.Now;
            user.LogTimes = 0;
            user.Phone = "";
            user.Points = 12;
            user.Question = "unknow";
            user.RegTime = DateTime.Now;
            user.Roles = 32;
            user.Sex = "男";
            user.SighWords = "一起和谐";

            String Path = "";
            Path = s_c_FilesManage.FilesManage.NewFriendsListXml(user.Name);
            if (Path == null)
            {
                ns_DatabaseAccess.c_DBA.SqlDataAccess.DeleteUser(user.Name);
                return false;
            }
            user.FriendsListXmlPath =Path ;

            Path = s_c_FilesManage.FilesManage.NewMsgGotRecordsXml(user.Name);
            if (Path == null)
            {
                ns_DatabaseAccess.c_DBA.SqlDataAccess.DeleteUser(user.Name);
                return false;
            }
            user.MsgGotRecordsXmlPath = Path;

            Path = s_c_FilesManage.FilesManage.NewMsgNotSentRecordsXml(user.Name);
            if (Path == null)
            {
                ns_DatabaseAccess.c_DBA.SqlDataAccess.DeleteUser(user.Name);
                return false;
            }
            user.MsgNotSentXmlPath = Path;

            Path = s_c_FilesManage.FilesManage.NewMsgSentRecordsXml(user.Name);
            if (Path == null)
            {
                ns_DatabaseAccess.c_DBA.SqlDataAccess.DeleteUser(user.Name);
                return false;
            }
            user.MsgSentRecordsXmlPath = Path;

            Path = s_c_FilesManage.FilesManage.NewImgHeadPath(user.Name);
            if (Path == null)
            {
                ns_DatabaseAccess.c_DBA.SqlDataAccess.DeleteUser(user.Name);
                return false;
            }
            user.ImgHeadPath = Path;

            Path = s_c_FilesManage.FilesManage.ImgDefaultHeadUrl(user.Name);
            if (Path == null)
            {
                ns_DatabaseAccess.c_DBA.SqlDataAccess.DeleteUser(user.Name);
                return false;
            }
            user.ImgHeadUrl = Path;

            if (!ns_DatabaseAccess.c_DBA.SqlDataAccess.AddUser(user)) return false; 
            else return true;
        }
        public Boolean Login(String name, String password)
        {
            c_User user = ns_DatabaseAccess.c_DBA.SqlDataAccess.GetUser(name);
            if (user == null) return false;
            if (user.HashedPassword.Equals(ns_SecurityManage.c_Hash.Md5(password))) return false;
            c_FriendsListXml flx = new c_FriendsListXml(user.FriendsListXmlPath);
            user.FriendsListCache = flx.FriendsListXml;
            c_SessionManage.SaveLoginUser(user);
            return true;
        }
        public Boolean IsUserLogin()
        {
            if (c_SessionManage.GetLoginUser() == null)
            {
                return false;
            }
            else return true;
        }
        public String GetFriendsList()
        {
            c_User user = c_SessionManage.GetLoginUser();
            return user.FriendsListCache;
        }

    }

}
